<!DOCTYPE html>
<!-- This test cannot be upstreamed to WPT because it asserts the behavior of
  the HTTP cache. This behavior does not violate any standard but neither is it
  required. -->
<title>Service Worker: Registration update() served from cache</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<script>
const scope = 'resources/scope/update-served-from-cache';
const worker_url = 'resources/update-served-from-cache-worker.php';

for (let update_via_cache of ['none', 'imports']) {
    promise_test(function(t) {
        let registration;

        let options = { updateViaCache: update_via_cache };
        return service_worker_unregister_and_register(t, worker_url, scope,
                                                      options)
          .then(function(r) {
              // The script resource for this register() having a non-zero
              // max-age value was cached.
              registration = r;
              return wait_for_state(t, registration.installing, 'activated');
            })
          .then(function() {
              // update() should consult the cache. The returned promise should
              // resolve when the script is retrieved from the cache.
              return registration.update();
            })
          .then(function() {
              // installing should not be null as update() triggers install.
              assert_not_equals(
                  registration.installing,
                  null,
                  'installing should not be null as the script should bypass ' +
                  'cache.');
              return service_worker_unregister_and_done(t, scope);
            });
      }, 'Update a registration bypassing cache with update_via_cache: ' +
         update_via_cache + '.');
}

for (let update_via_cache of ['all']) {
    promise_test(function(t) {
        let registration;

        let options = { updateViaCache: update_via_cache };
        return service_worker_unregister_and_register(t, worker_url, scope,
                                                      options)
          .then(function(r) {
              // The script resource for this register() having a non-zero
              // max-age value was cached.
              registration = r;
              return wait_for_state(t, registration.installing, 'activated');
            })
          .then(function() {
              // update() should consult the cache. The returned promise should
              // resolve when the script is retrieved from the cache.
              return registration.update();
            })
          .then(function() {
              // installing should be null as update() does not trigger install.
              assert_equals(
                  registration.installing,
                  null,
                  'installing should be null as the script was served from ' +
                  'cache.');
              return service_worker_unregister_and_done(t, scope);
            });
      }, 'Update a registration served from cache with update_via_cache: ' +
         update_via_cache + '.');
}
</script>
